<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;

class AdminController extends CommonController {
	protected $table = 'oo_admin';//管理员表
	protected $tp_agrade = 'admingroup';	//管理员组表	
	protected $tp_pset = 'urlset';	//后台设置表
	
	/*
	*
	*管理员列表
	*/	
	public function lists(){
		unset($str);
		unset($arr);
		unset($join);
		unset($table);
		unset($field);
		$table = '__OO_ADMIN__ AS ad' ;
		$str = "ad.id>%d" ;
		$arr = array(0) ;
		$join ['join1'] = 'LEFT JOIN __ADMINGROUP__ AS gr ON ad.gid=gr.id' ;
		$field = 'ad.*,gr.title,gr.gname,gr.createtime,gr.updatetime,gr.gr_status' ;
		$list = _RowsJoin($table, $join, $str, $arr, $field);
		
		unset($str);
		unset($arr);
		unset($field);
		//管理员权限 假如超级管理员，可以增修改，否则 只能查看		
		$str = "id>%d" ;
		$arr = array(0) ;
		$field = '*' ;
		
		$set_list = _getList($this->tp_agrade, $str, $arr,'','id desc','','',$field) ;
		//dump($list);die;
		$this->assign('set_list',$set_list);
		$this->assign('level',$_SESSION['gr_status']);//权限
		$this->assign('list',$list);
		$this->display();
	}
	
	/*
	*
	*添加管理员列表
	*/	
	public function addAdmin(){
		
		if (IS_POST) {
			
			if ($_SESSION['gr_status']!='2') {
				die("您没有操作权限");
			}
			
			unset($data);
			unset($res);
			unset($str);
			unset($arr);
			$type = I('post.type');
			switch ($type) {
				case 'admin_add':
					I('post.adminname')? $data['name'] = I('post.adminname') : $this->error('管理员账号不能为空！','lists');
					I('post.password')? $data['passwd'] = MD5(MD5(I('post.password'))) : $data['passwd'] = MD5(MD5('123456'));
					$data['status'] = 0 ;
					$data['passwd'] = _enPasswd($data['passwd']) ;					
					//管理组
					!I('post.gid') && $this->error('管理组不能为空！','lists');
					$data['gid'] = I('post.gid') ;
					
					$res = 1 ;
				break;
				case 'admin_edit':	
					I('post.adminname')? $data['name'] = I('post.adminname') : $this->error('管理员账号不能为空！','lists');
					I('post.password') && $data['passwd'] = MD5(MD5(I('post.password'))) ;
					$data['passwd'] && $data['passwd'] = _enPasswd($data['passwd']) ;
					//管理组
					!I('post.gid') && $this->error('管理组不能为空！','lists');
					$data['gid'] = I('post.gid') ;
					
					$str = "id=%d" ;
					$arr = array(I('post.id')) ;
					
					$res = 2 ;
				break;
				case 'admin_del':	
					$str = "id=%d" ;
					$arr = array(I('post.id')) ;
					
					$res = 3 ;
				break;				
			}
			
			if ($res == 1) {
				//添加
				$chenge = _getAdd($this->table,$data);
			} else if ($res == 2) {
				//修改
				$chenge = _getEdit($this->table,$str, $arr, $data);				
			} else if ($res == 3) {
				//删除
				$chenge = _getDel($this->table,$str, $arr);				
			}
			if ($res !== 3) {
				if ($chenge) {
					$this->success('操作成功！','lists') ;
				} else {
					$this->error('操作失败！','lists') ;
				}				
			} else {
				if ($chenge) {
					$this->ajaxReturn(array('info'=>1));
				} else {
					$this->ajaxReturn(array('info'=>0));
				}					
			}

		}
		
	}
	
	/*
	*
	*管理员组
	*/	
	public function group(){
		unset($str);
		unset($arr);
		//管理员权限 假如超级管理员，可以增修改，否则 只能查看
		
		$str = "id>%d" ;
		$arr = array(0) ;
		if ($_SESSION['gr_status']==='2') {
			$field = '*' ;
		} else {
			$field = 'gname,updatetime,gr_status,id' ;
		}
		$list = _getList($this->tp_agrade, $str, $arr,'','id desc','','',$field) ;
		//dump($list);die;
		
		$this->assign('list',$list);
		$this->display();
	}
	
	/*
	*
	*管理员组 增、删、改
	*/	
	public function group_updata(){
		unset($str);
		unset($arr);
		//管理员权限 假如超级管理员，可以增修改，否则 只能查看
		if ($_SESSION['gr_status']!='2') {
			die('您没有操作权限');
		}
		$type = I('post.type') ;
		unset($data);
		unset($res);
		switch ($type) {
			case 'add_group':
				I('post.group_name')?$data['gname'] = I('post.group_name'):die("<script>alert('管理组名称不能为空');history.go(-1);</script>");
				$data['createtime'] = time();
				$data['updatetime'] = time();
				$data['gr_status'] = 0;
				$res = 1 ;
			break;
			case 'edit_group':
				I('post.group_name')?$data['gname'] = I('post.group_name'):die("<script>alert('管理组名称不能为空');history.go(-1);</script>");
				$data['updatetime'] = time();
				$res = 2 ;
			break;
			case 'del_group':
				$res = 3 ;
			break;
		}
		
		if ($res===1) {
			$list = M($this->tp_agrade)->where('gname="%s"',array($data['gname']))->field('gname')->find();
		} else if ($res===2) {
			unset($list);
			$list = M($this->tp_agrade)->where('gname="%s" and id<>%d',array($data['gname'],I('post.id')))->field('gname')->find();			
		}
		if ($list) {
			die("<script>alert('管理组名称已存在');history.go(-1);</script>");
		}	
		unset($list);
		
		if ($res===1) {
			$change = M($this->tp_agrade)->add($data);			
			$msg[1] = '添加成功' ;
			$msg[2] = '添加失败' ;
		} else if ($res===2) {
			$change = M($this->tp_agrade)->where("id=%d",array(I('post.id')))->save($data);
			$msg[1] = '修改成功' ;
			$msg[2] = '修改失败' ;
		}
		
		if ($res > 0 && $res < 3) {
			if ($change) {
				$this->success($msg[1]);
			} else {
				$this->error($msg[2]);
			}			
		}
			
	}
	
	/*
	*
	*查看管理组权限
	*/
	 public function pow_set() {
		if (I('get.atc')==='set') {
			unset($str);
			unset($arr);
			//管理员权限 假如超级管理员，可以增修改，否则 只能查看
			
			$str = "set_pid>%d" ;
			$arr = array(0) ;
			
			$field = 'set_name,set_url' ;
			
			$list = _getList($this->tp_pset, $str, $arr,'','set_id desc','','',$field) ;
			
			unset($url) ;
			$url = M($this->tp_agrade)->where('id=%d',array(I('get.id')))->field('gr_status,rules')->find();
			
			if ($url['rules']) {
				$url_arr = unserialize ($url['rules']) ;
			}
			//dump($url_arr);die;
			$this->assign('id',I('get.id'));
			$this->assign('list',$list);
			$this->assign('set_pow',$url);
			$this->assign('url_arr',$url_arr);
			
			$this->display();
		}		
	}
	
	/*
	*
	*修改管理组权限
	*/
	 public function pow_updata() {
		if (I('post.atc')==='set_updata') {
			
			if ($_SESSION['gr_status']!='2') {
				die('您没有操作权限');
			}
			$list = M($this->tp_agrade)->where("id=%d",array(I('post.id')))->field('rw_change')->find();
			if ($list['rw_change']==='1') {
				die("<script>alert('超级管理员权限不可修改权限');history.go(-1);</script>");
			}
			unset($set_url);
			unset($data);
			$set_url = serialize(I('post.set_url')) ;
			$data['updatetime'] = time();
			$data['rules'] = $set_url;
			$data['gr_status'] = 1;
			$change = M($this->tp_agrade)->where("id=%d",array(I('post.id')))->save($data);
			if ($change) {
				$this->success('修改成功','group');
			} else {
				die("<script>alert('修改失败');history.go(-1);</script>");
			}
		}		
	}
		
}